let GoldGiftId;
layui.config({
    base: '../../js/layui/lay/modules/'
}).use(['form','laydate', 'table', 'layer', 'element','upload','laypage'],function(){
    form = layui.form;
    layer = layui.layer;
    laypage = layui.laypage;
    laydate = layui.laydate;//日期
    table = layui.table;
    element = layui.element; //元素操作
    upload = layui.upload;//上传
    //日期时间范围
    laydate.render({
        elem: '#times'
        , type: 'datetime'
        , range: true
        ,trigger: 'click'
    });
    TypelistByTypeId(2);
    selectAllProductInfoByInfoPc();
    GoldGiftId=GetQueryString("GoldGiftId");
    if(!isNotNull(GoldGiftId)){
        GoldGiftId=0;
        $(".add").show();
        $(".update").hide();
    }else{
        GoldGiftlistById();
        $(".add").hide();
        $(".update").show();
    }
    $(".add_rules").click(function(){
        $(this).after(rule);
    });
});
function submitInfo() {
    if(listId.length==0){
        layer.alert("请选择要参与活动的商品!!");
        return;
    }
    ProductListByTypeIdInfo(listId);
}
function ProductListByTypeIdInfo(listId) {
    var info='<table class="table table-bordered text-center" id="tableInfo"><thead>' +
        '<tr>' +
        '<th>商品ID</th>' +
        '<th>商品名称</th>' +
        '<th>商品图片</th>' +
        '<th>剩余数量</th>' +
        '<th>需要金币数量</th>' +
        '<th>操作</th>' +
        '</tr>' +
        '</thead>' +
        '<tbody>';
    $.ajax({
        url: httpurl+"/ProductListByTypeIdInfo?proIdList="+listId,
        cache: false,
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType : 'application/json',
        success: function(data) {
            let dataList=data.data;
            for(let i=0;i<dataList.length;i++) {
                info+='<tr class="plist">' +
                    '<td class="pid">'+dataList[i].id+'</td>' +
                    '<td class="name">'+dataList[i].title+'</td>' +
                    '<td class="width20 img"><img width="100" name="'+dataList[i].headimg+'" src="'+visitimgurl+"/"+dataList[i].headimg+'" /></td>' +
                    '<td><input type="text" class="form-control form-boxed width80 num"/></td>' +
                    '<td><input type="text" class="form-control form-boxed width80 goldnum"/></td>' +
                    '<td><i class="icon-remove red" onclick="iconremove(this)"></i></td>' +
                    '</tr>';
            }
            info+='</tbody></table>';
            $("#AllChooseList").html(info);
        }
    });
}

//查询商品
function GoldGiftlistById(){
    $.ajax({
        url: httpurl+"/GoldGiftById",
        cache: false,
        data: {
            "id":GoldGiftId
        },
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType : 'application/json',
        success: function(data){
            if(data.rtnCode==200) {
                let dataList=data.data;
                $("#giftname").val(dataList.giftname);
                $('.isshelves input[value="' + dataList.isground + '"]').attr('checked', 'true');
                $('#sort').val(dataList.sort);
                $("#number").val(dataList.number);
                $(".enable input[name='enable'][value="+dataList.enable+"]").attr("checked",true);
                if (dataList.image != null && dataList.image != "" && dataList.image != undefined) {
                    $("#preview").attr("src", visitimgurl + "/" + dataList.image);
                    $("#preview").attr("name", dataList.image);
                    $("#divpreview").show();
                }
                $("#times").val(dataList.starttime+" - "+dataList.endtime);
                var info='<table class="table table-bordered text-center" id="tableInfo"><thead>' +
                    '<tr>' +
                    '<th>商品ID</th>' +
                    '<th>商品名称</th>' +
                    '<th>商品图片</th>' +
                    '<th>剩余数量</th>' +
                    '<th>需要金币数量</th>' +
                    '<th>操作</th>' +
                    '</tr>' +
                    '</thead>' +
                    '<tbody><tr class="plist">' +
                        '<td class="pid">'+dataList.pid+'</td>' +
                        '<td class="name">'+(isNotNull(dataList.productThreeIdHeadTitle)?dataList.productThreeIdHeadTitle.title:"")+'</td>' +
                        '<td class="img"><img name="'+(isNotNull(dataList.productThreeIdHeadTitle)?dataList.productThreeIdHeadTitle.headimg:"")+'" src="'+visitimgurl + "/" +(isNotNull(dataList.productThreeIdHeadTitle)?dataList.productThreeIdHeadTitle.headimg:"")+'" width="60"/></td>' +
                        '<td><input type="text" class="form-control form-boxed width80 num" value="'+dataList.number+'"/></td>' +
                        '<td><input type="text" class="form-control form-boxed width80 goldnum" value="'+dataList.goldnum+'"/></td>' +
                        '<td><i class="icon-remove red" onclick="iconremove(this)"></i></td>' +
                        '</tr></tbody></table>';
                $("#AllChooseList").html(info);
            }
        }

    });
}
function iconremove(obj) {
    $(obj).parent().parent().remove();
}
//提交商品
function submitInfos(){
    let giftname=$("#giftname").val();
    var enable=$("input[name='enable']:checked").val();
    let sort=$('#sort').val();
    if(sort=="")sort=0;
    var map="";
    $("#tableInfo .plist").each(function () {
        var pid=$(this).find(".pid").html();
        var num=$(this).find(".num").val();
        var goldnum=$(this).find(".goldnum").val();
        var name=$(this).find(".name").html();
        var img=$(this).find(".img img").attr("name");
        map+='{"pid":'+pid+',"num":'+num+',"goldnum":'+goldnum+',"name":"'+name+'","img":"'+img+'"},';
    });
    if(map.length==0){
        top.layer.alert("请至少设置一个商品！");
        return;
    }
    var list="["+map.substring(0,map.length-1)+"]";
    let times = $("#times").val();
    var starttime="";
    var endtime="";
    if (""!=times){
        starttime=times.split(" - ")[0];
        endtime=times.split(" - ")[1];
    }

    top.layer.confirm('确认提交，如果提交的商品已经存在了,将会覆盖原来设置的商品信息？',{icon: 3,title:'提示信息'},function(index){
        var index = top.layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.ajax({
            type:"POST",
            url: httpurl+"/submitGiftWatch",
            cache: false,
            data: JSON.stringify({
                "id":GoldGiftId,
                "giftname":giftname,
                "list":list,
                "enable":enable,
                "sort":sort,
                "starttimes":starttime,
                "endtimes":endtime
            }),
            headers: {
                "Access-Control-Allow-Origin": "*",
                "Access-Control-Allow-Methods": "*",
                "Access-Control-Allow-Headers": "Authorization",
                "Authorization": GetTokenInfo()
            },
            contentType : 'application/json',
            success: function(data) {
                layer.closeAll();
                top.layer.msg(data.msg);
                if(data.rtnCode==200){
                    return_list();
                }
            }
        });
    },function(){

    });
}
